package main

import (
	"net/http"
	"global/core"
	"strings"
	"global/libs/http/app"
	"github.com/ngaut/log"
	"global/libs/structs"
	"global/util"
)

func (h *Handler) OnHandleGetExchangeDetail(writer http.ResponseWriter, request *http.Request)  {
	h.writer = writer
	symbol := strings.ToLower(request.FormValue("slug"))
	if symbol == "" {
		h.Error(app.ErrParameter)
		return
	}

	d, ErrorCode := h.getExchangeDetailData(symbol)
	if ErrorCode != app.ErrNone {
		h.Error(ErrorCode)
		return
	}

	response := app.AppMsg(d)
	h.Json(response)
}

func (h *Handler) getExchangeDetailData(symbol string) (*structs.ExchangeBase, app.AppErr) {
	conn := h.ctx.GetBaseRedis().Get()
	defer conn.Close()

	meta, err := core.GetAnalysisExchangeMetaData(conn, symbol)
	if err != nil {
		log.Errorf("Error: %s", err)
		return nil, app.ErrExchangeNotExists
	}

	meta.Logo = util.ImageUrl(h.ctx.GetImgUrl(), meta.Logo)

	return meta, app.ErrNone
}